﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using control.Dal;

namespace control.Biz
{
    public class BizPhieuNhap
    {
        public int ID { get; set; }
        public string Ma { get; set; }
        public DateTime NgayNhap { get; set; }
        private List<BizChiTietPhieuNhap> items;
        public List<BizChiTietPhieuNhap> Items
        {
            set { items = value; }
            get
            {
                if (items == null)
                {
                    items = new List<BizChiTietPhieuNhap>();
                }
                return items;
            }
        }

        public static List<BizPhieuNhap> GetAll()
        {
            return DalPhieuNhap.GetAll();
        }



        public void Insert()
        {
            ID = DalPhieuNhap.Insert(this);
            foreach (BizChiTietPhieuNhap item in Items)
            {
                item.PhieuNhap = this;
                item.Inset();
            }
        }

        public bool ItemsContains(BizChiTietPhieuNhap _item)
        {
            foreach (BizChiTietPhieuNhap item in Items)
            {
                if (item.Sach.IdSach == _item.Sach.IdSach)
                    return true;
            }
            return false;
        }

        public void Delete()
        {
            BizChiTietPhieuNhap.DeleteCTofPhieu(this);
            DalPhieuNhap.Delete(this);

        }

        public void Update()
        {
            DalPhieuNhap.Update(this);
            BizChiTietPhieuNhap.DeleteCTofPhieu(this);
            foreach (BizChiTietPhieuNhap item in Items)
            {
                item.PhieuNhap = this;
                item.Inset();
            }
        }

        public static List<BizPhieuNhap> Find(string p, DateTime FromDateTime, DateTime ToDateTime)
        {
            DateTime newFromDateTime = new DateTime(FromDateTime.Year, FromDateTime.Month, FromDateTime.Day, 0, 0, 1);
            DateTime newToDateTime = new DateTime(ToDateTime.Year, ToDateTime.Month, ToDateTime.Day, 23, 59, 59);
            return DalPhieuNhap.Find(p, newFromDateTime, newToDateTime);
        }
    }
}
